<template>
  <div style="padding: 20px">
    <el-row>
      <el-col>
        <el-form ref="sysLogForm" :model="sysLog" label-width="80px" :inline="true">
          <el-form-item label="关键字" prop="keyWord">
            <el-input v-model="sysLog.keyWord" placeholder="请输入搜索条件" size="small" />
          </el-form-item>
          <el-form-item label="请求方法" prop="requestMethod">
            <el-select v-model="sysLog.requestMethod" size="small">
              <el-option label="GET请求" value="GET" />
              <el-option label="POST请求" value="POST" />
              <el-option label="PUT请求" value="PUT" />
              <el-option label="DELETE请求" value="DELETE" />
            </el-select>
          </el-form-item>
          <el-form-item label="操作类型" prop="operaCode">
            <el-select v-model="sysLog.operaCode" size="small">
              <el-option label="新增" value="ADD" />
              <el-option label="更新" value="UPDATE" />
              <el-option label="删除" value="DELETE" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="onSearch">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="refresh">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-table v-loading="loading" :data="tableData" border style="width: 100%">
          <el-table-column type="selection" width="30" prop="id" align="center" />
          <el-table-column prop="operaModel" label="操作的模块" align="center" />
          <el-table-column prop="operaDesc" label="操作说明" align="center" />
          <el-table-column prop="operaCode" align="center" label="操作标识" />
          <el-table-column prop="operaType" align="center" label="操作类型" />
          <el-table-column prop="requestMethod" align="center" label="请求方式" />
          <el-table-column prop="requestUrl" align="center" label="请求地址" />
          <el-table-column prop="requestIp" align="center" label="请求ip" />
          <el-table-column prop="requestIp" align="center" label="请求ip" />
          <el-table-column prop="operaStatus" align="center" label="操作状态">
            <template slot-scope="scope">
              {{ scope.row.operaStatus ? '成功' : '失败' }}
            </template>
          </el-table-column>
          <el-table-column prop="operaCostTime" align="center" label="请求耗时/秒" />
          <el-table-column prop="operaNickName" align="center" label="操作人名称" />
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!-- 表格下方放置一个分页组件，并使其水平居中 -->
    <el-row type="flex" justify="center" style="margin-top: 20px">
      <el-col :span="12">
        <!-- 根据需要调整 span 值 -->
        <!-- 表格下方放置一个分页组件 -->
        <el-pagination
          :current-page="page"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { pageList, deleteById } from '@/api/log'
  export default {
    name: 'Log',
    data() {
      return {
        loading: false, // 表格加载状态
        total: 0, // 总记录数
        page: 1, // 当前页码
        pageSize: 5, // 每页记录数
        tableData: [], // 表格数据
        sysLog: {
          keyWord: '',
          requestMethod: '',
          operaCode: ''
        }
      }
    },
    created() {
      this.pageList()
    },
    methods: {
      /**
       * 获取岗位列表信息
       */
      async pageList() {
        this.loading = true
        const { total, list } = await pageList(this.page, this.pageSize, this.sysLog)
        this.total = total
        this.tableData = list
        this.loading = false
      },
      onSearch() {
        this.pageList()
      },
      refresh() {
        this.$refs.sysLogForm.resetFields()
        this.pageList()
      },
      /**
       * 选择每页显示多少条时候触发
       * @param pageSize
       */
      handleSizeChange(pageSize) {
        // console.log(`每页 ${val} 条`)
        this.pageSize = pageSize
        this.pageList()
      },
      /**
       *点击页码数发生变化时候触发
       * @param page
       */
      handleCurrentChange(page) {
        // console.log(`当前页: ${val}`)
        this.page = page
        this.pageList()
      },
      handleDelete(id) {
        this.$confirm('此操作将永久删除该岗位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            await deleteById(id)
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.pageList()
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
      }
    }
  }
</script>

<style></style>
